package cn.hiapi.shop.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.jpa.pageable.PageRequest;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.token.Token;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shop.query.ShopMessageQuery;
import cn.hiapi.shop.service.shop.ShopLinkUserService;
import cn.hiapi.user.api.query.notify.NotifyMessageQuery;
import cn.hiapi.user.api.vo.notify.NotifyMessageBodyVo;
import cn.hiapi.user.api.vo.notify.NotifyMessageVo;
import cn.hiapi.user.entity.notiy.NotifyMessage;
import cn.hiapi.user.entity.notiy.NotifyOrientation;
import cn.hiapi.user.entity.notiy.NotifyView;
import cn.hiapi.user.enums.NotifySource;
import cn.hiapi.user.service.notify.NotifyMessageService;
import cn.hiapi.user.service.notify.NotifyOrientationService;
import cn.hiapi.user.service.notify.NotifyViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2023/2/5 22:07
 */
@RestController
@RequestMapping({"/shop/message"})
public class MessageController extends BasicQueryController<NotifyOrientation, Long, NotifyMessageBodyVo, ShopMessageQuery> {
    private final NotifyMessageService notifyMessageService;
    private final NotifyViewService notifyViewService;
    private final NotifyOrientationService notifyOrientationService;

    protected MessageController(NotifyOrientationService basicService, NotifyMessageService notifyMessageService, NotifyViewService notifyViewService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
        notifyOrientationService = basicService;
        this.notifyMessageService = notifyMessageService;
        this.notifyViewService = notifyViewService;
    }

    @Override
    public ShopMessageQuery setQuery(ShopMessageQuery query) {
        Token<Object> token = TokenGet.get();
        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(token.getFid(), query.getShopId());
        return super.setQuery(query);
    }

    @Override
    public List<NotifyMessageBodyVo> toListVo(List<NotifyOrientation> list, String[] fields) {
        final List<NotifyMessageBodyVo> collect = list.stream().map(e -> {
            NotifyMessageBodyVo vo = new NotifyMessageBodyVo();
            final NotifyMessage message = notifyMessageService.get(e.getNotifyId());
            BeanUtils.copyProperties(message, vo);
            if (e.getUid() != null) {
                final NotifyView view = this.notifyViewService.getByUid(e.getUid(), e.getNotifyId());
                if (view != null) vo.setRead(1);
            }
            vo.setId(e.getId());
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public NotifyMessageBodyVo toData(NotifyOrientation data, String[] fields) {
        NotifyMessageBodyVo vo = new NotifyMessageBodyVo();
        final NotifyMessage message = notifyMessageService.get(data.getNotifyId());
        BeanUtils.copyProperties(message, vo);
        Token<Object> token = TokenGet.get();
        if (token != null && data.getUid() != null)
            this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(token.getFid(), data.getUid());
        if (data.getUid() != null)
            this.notifyViewService.viewByUid(data.getUid(), data.getNotifyId());
        return vo;
    }


    @ApiOperation(value = "没读消息数量", notes = "没读消息数量", nickname = "AdinZ")
    @GetMapping("/message_count")
    public ResponseEntity<Long> messageCount(@RequestParam("shopId") long shopId) {
        Token<Object> token = TokenGet.get();
        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(token.getFid(), shopId);
        final NotifyMessageQuery query = new NotifyMessageQuery();
        query.setUid(shopId);
        query.setSize(100);
        final ResponseEntity<List<NotifyOrientation>> response = this.notifyOrientationService.findPage(query);
        if (response.getData().size() == 0) return toSuccess(0L);
        final List<Long> collect = response.getData().stream().map(NotifyOrientation::getNotifyId).collect(Collectors.toList());
        long count = this.notifyViewService.count(new QueryWrapper().eq("uid", query.getUid()).in("notifyId", collect));
        return toSuccess(collect.size() - count);
    }


}